#ifndef DEMUXER_H
#define DEMUXER_H

#include "yuvfilespec.h"
#include "pcmfilespec.h"
#include <QDebug>
#include <QFile>

extern "C" {

    #include <libavutil/avutil.h>
}

class Demuxer
{

public:
    Demuxer();
    int demuxer(const char* videoPath,YUVFileSpec& yuvFileSpec, PCMFileSpec& pcmFileSpec);

private:
    YUVFileSpec* _yuvFileSpec = nullptr;
    PCMFileSpec* _pcmFileSpec = nullptr;
    AVFormatContext* _fmtContext = nullptr;
    AVCodecContext* _audioCodecCxt = nullptr;
    AVCodecContext* _videoCodecCxt = nullptr;
    int _audioIndex;
    int _videoIndex;

    QFile* _videoOutFile = nullptr;
    QFile* _audioOutFile = nullptr;

    AVFrame* _frame = nullptr;
    AVPacket* _packet = nullptr;

    u_int8_t* _imageBuffer[4] = {nullptr};
    int _lineSizes[4] = {0};

    int init(AVMediaType mediaType,int* mediaIndex,AVCodecContext** codecCxt);
    int initVideo();
    int initAudio();

    int decode(AVCodecContext *_codecCxt, AVPacket* packet, void (Demuxer::*func)(void));

    void decodeVideo();
    void decodeAudio();
};

#endif // DEMUXER_H
